<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>登录</title>
    <link rel="stylesheet" href="../assert/layui/css/layui.css" />
    <style>
        .login {
            min-width: 300px;
            width: 50%;
            margin: 20px auto;
        }
    </style>
</head>

<body>

    <div class="login">
        <form class="layui-form" action="###">
            <div class="layui-form-item">
                <label class="layui-form-label">用户名</label>
                <div class="layui-input-block">
                    <input type="text" name="user_name" required lay-verify="required" placeholder="请输入你的用户名"
                        autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">密码框</label>
                <div class="layui-input-block">
                    <input id="pwd" type="password" name="user_pwd" required lay-verify="userpwd"
                        placeholder="最少1个大写字母，1个小写字母，1个数字，1个特殊字符" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit lay-filter="formDemo">登录</button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </div>
        </form>
    </div>
    <script src="../assert/layui/layui.js"></script>
    <script src="../assert/request.js"></script>
    <script>
        layui.use('form', function () {
            let form = layui.form;
            // 验证密码
            form.verify({
                userpwd(value, item) {
                    let userpwdReg = /^.*(?=.{6,})(?=.*\d)(?=.*[A-Z])(?=.*[a-z])(?=.*[!@#$%^&*? ]).*$/
                    if (!userpwdReg.test(value)) {
                        return '密码不符合规则'
                    }
                }
            })
            //监听提交
            form.on('submit(formDemo)', function (data) {
                request('/user/login', {
                    method: 'POST',
                    body: data.field
                }).then(data => {
                    if(data.code === 1){
                        localStorage.setItem('token',data.token);
                        location.href = 'index.html';
                    }
                })
                return false;
            });
        })

    </script>
</body>

</html>